package com.graduate.common.service.impl;

import com.alibaba.druid.sql.PagerUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.graduate.common.entity.House;
import com.graduate.common.mapper.HouseMapper;
import com.graduate.common.service.IHouseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.graduate.common.utils.PageUtils;
import com.graduate.common.vo.HouseVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zfy
 * @since 2021-05-09
 */
@Service
public class HouseServiceImpl extends ServiceImpl<HouseMapper, House> implements IHouseService {

    @Override
    public IPage<HouseVo> selectHousePage(Map<String, Object> map) {
        // 设置分页查询对象
        IPage<House> page = (IPage<House>) PageUtils.convertToPage(map);
        QueryWrapper<House> wrapper = new QueryWrapper<>();
        if (map.get("houseState") != null && !"".equals(map.get("houseState"))) {
            wrapper.eq("houseState", map.get("houseState"));
        }
        // page为查询出的分页对象
        page = baseMapper.selectPage(page, wrapper);
        // 取出page中的数据转换成VO对象
        List<House> records = page.getRecords();
        List<HouseVo> houseVos = records.stream().map(record -> {
            HouseVo houseVo = new HouseVo();
            // 把从数据库中查询出来的对象中的属性拷贝到houseVo对象中
            BeanUtils.copyProperties(record, houseVo);
            return houseVo;
        }).collect(Collectors.toList());
        Page<HouseVo> ans = new Page<>();
        BeanUtils.copyProperties(page, ans);
        ans.setRecords(houseVos);
        return ans;
    }
}
